<?php

namespace App\Api\Logistics;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use App\Repository\Worker\ReporyOrderRepository;

class TransferLogistics
{
  public $model = null;
  private $root;
  public function __construct()
  {
    $this->root = new Logger('TransferLogistics');
    $this->root->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/TransferLogistics.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  public function send($data)
  {
    if (empty($data['name'])) {
      return $this->returnMsg(101, '请传递通道名称');
    }
    // $channelArr = DB::table(Prefix . 'wlksmschannel')->where(['name' => $data['name']])->first();
    // if (empty($channelArr)) {
    //   return $this->returnMsg(104, '未查询到通道');
    // }
    // if (empty($channelArr->name)) {
    //   return $this->returnMsg(105, '通道配置错误');
    // }
    // $pname = $channelArr->name;
    $model = new KChannel();
    $this->model = $model->loadChannel($data['name']);
    if (empty($this->model)) {
      return $this->returnMsg(105, '未查询到通道');
    }
    return $this->returnMsg(200, '');
  }
  public function __call($name, $arguments)
  {
    $data = !empty($arguments[0]) ? $arguments[0] : [];
    $sms_way = '';
    $isTrue = DB::table('system')->where(['name' => 'is_get_express'])->first();
    if (empty($isTrue) || empty($isTrue->val)) {
      return $this->returnMsg(100, '物流查询功能未开启');
    }
    $model = $this->send($data);

    if (!empty($model['code']) && $model['code'] == 200) {
      $res = $this->model->$name($data);
      return $res;
    }
  }
  public function returnMsg($code = 200, $msg = '', $data = [])
  {
    return [
      'code' => $code,
      'msg' => $msg,
      'data' => $data
    ];
  }
}
